package com.xydtech.ecard.merchant.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * JWDUtils校验经纬度是否在网格经纬度范围内
 *
 * @author yuzhiguo
 * @date 2024-11-05 上午10:26
 */
public class JWDUtils {

    /**
     * 判断某一个经纬度点是否在一组经纬度范围内
     *
     * @param ALon A点经度
     * @param ALat A点纬度
     * @param ps   范围多边形经纬度集合
     * @author Klay
     * @date 2023/2/8 18:06
     */
    public static boolean isPtInPoly(double ALon, double ALat, List<JqPoint> ps) {
        int iSum, iCount, iIndex;
        double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;
        if (ps.size() < 3) {
            return false;
        }
        iSum = 0;
        iCount = ps.size();
        for (iIndex = 0; iIndex < iCount; iIndex++) {
            if (iIndex == iCount - 1) {
                dLon1 = ps.get(iIndex).getX();
                dLat1 = ps.get(iIndex).getY();
                dLon2 = ps.get(0).getX();
                dLat2 = ps.get(0).getY();
            } else {
                dLon1 = ps.get(iIndex).getX();
                dLat1 = ps.get(iIndex).getY();
                dLon2 = ps.get(iIndex + 1).getX();
                dLat2 = ps.get(iIndex + 1).getY();
            }
            // 以下语句判断A点是否在边的两端点的水平平行线之间，在则可能有交点，开始判断交点是否在左射线上
            if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1))) {
                if (Math.abs(dLat1 - dLat2) > 0) {
                    //得到 A点向左射线与边的交点的x坐标：
                    dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat)) / (dLat1 - dLat2);
                    // 如果交点在A点左侧（说明是做射线与 边的交点），则射线与边的全部交点数加一：
                    if (dLon < ALon) {
                        iSum++;
                    }
                }
            }
        }
        if ((iSum % 2) != 0) {
            return true;
        }
        return false;
    }

    /**
     * 坐标转换，腾讯地图转换成百度地图坐标
     * @param lat 腾讯纬度
     * @param lon 腾讯经度
     * @return 返回结果：经度,纬度
     */
    public static String map_tx2bd(double lat, double lon){
        double bd_lat;
        double bd_lon;
        double x_pi=3.14159265358979324;
        double x = lon, y = lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        bd_lon = z * Math.cos(theta) + 0.0065;
        bd_lat = z * Math.sin(theta) + 0.006;
        return bd_lat+","+bd_lon;
    }

    /**
     * 坐标转换，百度地图坐标转换成腾讯地图坐标
     * @param lat  百度坐标纬度
     * @param lon  百度坐标经度
     * @return 返回结果：纬度,经度
     */
    public static String map_bd2tx(double lat, double lon){
        double tx_lat;
        double tx_lon;
        double x_pi=3.14159265358979324;
        double x = lon - 0.0065, y = lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        tx_lon = z * Math.cos(theta);
        tx_lat = z * Math.sin(theta);
        return tx_lat+","+tx_lon;
    }

    public static void main(String[] args) {
        List<JqPoint> ps = new ArrayList<>();
        String wgjwd = "116.74467086791992,35.488349662974507; 116.74140930175781,35.492402962011823; 116.74037933349608,35.513923951527694; 116.69265747070312,35.496735572810749; 116.68956756591795,35.493521077488872; 116.66793823242189,35.44654683290446; 116.68081283569336,35.422315017990975; 116.68699264526366,35.422454905089687; 116.72917842864989,35.4225598202543; 116.73050880432127,35.422734678558363; 116.7304229736328,35.429588825008153; 116.74806118011473,35.436827009627493; 116.74213886260985,35.460704906633971; 116.74192428588867,35.468744185014543; 116.74943447113036,35.482409113979287; 116.74471378326416,35.488384606081269; 116.74471378326416,35.488384606081269; 116.74467086791992,35.488349662974507";
        for (String jwd : wgjwd.split(";")) {
            String[] split1 = jwd.split(",");
            JqPoint jqPoint = new JqPoint(Double.valueOf(split1[0]), Double.valueOf(split1[1]));
            ps.add(jqPoint);
        }

        JqPoint n1 = new JqPoint(116.675556,35.463346);
        System.out.println("n1:" + isPtInPoly(n1.getX(), n1.getY(), ps));
    }
}
